import mysql = require("mysql")
import ConfigData from "../Module/ConfigData";
import GlobDef from "../Module/GlobDef";
import LogFile from "../Module/LogFile";


/*===================================================
管理数据库连接模块, 初始化多条连接应对
===================================================*/

export default class MySqlCenter{
    public static pCenter: MySqlCenter = null;
    public static getInstance(): MySqlCenter {
        if (MySqlCenter.pCenter == null) {
            MySqlCenter.pCenter = new MySqlCenter();
        }
        return MySqlCenter.pCenter;
    }

    public m_vMySqlLinks:any[]  = [];   //重复用,去一次到最后队列
    
    public InitConnect(num:number){
        var pDBCfg = ConfigData.getInstance().server_json["db"];
        for(var i = 0; i < num; i++){
            var pMySqlL = mysql.createConnection(pDBCfg);
            pMySqlL.connect();
            this.m_vMySqlLinks.push(pMySqlL);
        }
        GlobDef.OutPut("MySql Link create ="+num);
//         this.m_pMySql = mysql.createConnection(pDBCfg);

    }

    public GetLink():any{
        if(this.m_vMySqlLinks.length == 0){
            GlobDef.OutError("Mysql not Init")
            return;
        }
        
        var pRtl = this.m_vMySqlLinks[0];
        this.m_vMySqlLinks.splice(0,1);
        this.m_vMySqlLinks.push(pRtl);
        return pRtl;
    }
}